<?php
//读取env配置
$env = [];
array_map(function ($item)use (&$env){
    $item = explode('=',$item);
    $value = isset($item[1])?$item[1]:'';
    if($value=='false'){
        $value = '';
    }
    $env[$item[0]] = str_replace(['"',"'"],'',$value);
},explode("\n",file_get_contents('.env')));
$is_online = isset($env['APP_ENV']) && $env['APP_ENV'] && $env['APP_ENV']!='local';
$composer_install = false;
$cache_file = './storage/app/envoy/cache.json';
$cache = ['composer.json'=>''];
if(file_exists($cache_file)){
    $cache = json_decode(file_get_contents($cache_file),true)?:[];
}
$md5_composer = md5(file_get_contents('./composer.json'));
if($cache['composer.json']!=$md5_composer){
    $composer_install = true;
    $cache['composer.json'] = $md5_composer;
}
file_put_contents($cache_file,json_encode($cache));
?>
@servers(['local' => '127.0.0.1'])

@task('update', ['on' => ['local']])
@if($path)
    cd /var/www/laravel/{{$path}} && \
@endif
php artisan down --redirect="/503" --retry=60 && \
@if($branch)
    git pull origin {{$branch}} && \
@elseif($is_online)
    git pull origin master && \
@else
    git pull origin dev && \
@endif
@if($composer_install)
composer install --optimize-autoloader @if($is_online) --no-dev @endif && \
@endif
php artisan migrate:all && \
php artisan db:seed --class=VersionSeeder --force && \
php artisan queue:restart && \
@if($path)
    php /var/www/laravel/{{$path}}/artisan build:index.html && \
    chmod -R 777 /var/www/laravel/{{$path}}/storage/framework/views && \
@else
    php artisan build:index.html && \
    chmod -R 777 storage/framework/views && \
@endif
@if($is_online)
    php artisan config:cache && \
    php artisan route:cache && \
@endif
@if($is_online && isset($env['SWOOLE_HTTP_HOST']) && $env['SWOOLE_HTTP_HOST'])
    php artisan swoole:http reload && \
@endif
php artisan up

@endtask

@task('init', ['on' => ['local']])
@if($path)
    cd /var/www/laravel/{{$path}} && \
@endif
@if($branch)
    git checkout {{$branch}} && \
    git pull origin {{$branch}} && \
@elseif($is_online)
    git checkout master && \
    git pull origin master && \
@else
    git checkout dev && \
    git pull origin dev && \
@endif
chmod -R 777 storage && \
chmod -R 777 bootstrap/cache && \
@if($unzip_vendor)
    unzip -o -d ./ ./vendor.zip && \
@endif
composer install --optimize-autoloader @if($is_online) --no-dev @endif && \
php artisan key:generate --force && \
php artisan storage:link && \
php artisan db:seed --class=CheckDatabaseSeeder --force && \
php artisan migrate:all && \
php artisan db:seed --force && \
@if($is_online)
    php artisan config:cache && \
    php artisan route:cache && \
@endif
@if($path)
    php /var/www/laravel/{{$path}}/artisan build:index.html && \
    chmod -R 777 /var/www/laravel/{{$path}}/storage/framework/views
@else
    php artisan build:index.html && \
    chmod -R 777 storage/framework/views
@endif
@endtask

